package my.assistant.redialer;

import my.assistant.OwnStatus;
import my.assistant.R;
import my.assistant.util.ResKey;
import my.assistant.util.ResUtils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Redialer extends Activity
{

	private static OwnStatus status = new OwnStatus();

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.redialer);
		status.firstDial = true;
		status.redial = ResUtils.getBooleanRes(ResKey.REDIALER_OPT_REDIAL);
//		Log.e("init", "----------------");
		final EditText dialNum = (EditText) findViewById(R.id.dialNum);
		ListView listView = (ListView) findViewById(R.id.listView);
		ListAdapter adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, new String[]{String.valueOf(getText(R.string.redialable))});
		listView.setAdapter(adapter);
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		listView.setItemChecked(0, status.redial);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
		{

			@Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
				CheckedTextView view = (CheckedTextView)arg1;
				if (view.getText().toString().equals(getText(R.string.redialable)))
                {
	                status.redial = view.isChecked() == false;
	                ResUtils.setRes(ResKey.REDIALER_OPT_REDIAL, status.redial);
                }
            }
		});
		final Intent dial = new Intent(Intent.ACTION_CALL, Uri.parse("tel://"));
		TelephonyManager telMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
		Button dialBtn = (Button) findViewById(R.id.dialBtn);
		dialBtn.setOnClickListener(new Button.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				status.firstDial = false;
				startDial(dialNum, dial);
			}

		});
		telMgr.listen(new PhoneStateListener()
		{
			@Override
			public void onCallStateChanged(int state, String incomingNumber)
			{
				super.onCallStateChanged(state, incomingNumber);
				switch (state)
				{
				case TelephonyManager.CALL_STATE_IDLE:
					if (status.firstDial == false)
					{
						Toast.makeText(Redialer.this, R.string.callIdle, Toast.LENGTH_LONG).show();
						if (status.redial)
						{
							startDial(dialNum, dial);
						}
					}
					break;
				case TelephonyManager.CALL_STATE_OFFHOOK:
					Toast.makeText(Redialer.this, R.string.callTalk, Toast.LENGTH_LONG).show();
					break;
				case TelephonyManager.CALL_STATE_RINGING:
					Toast.makeText(Redialer.this, R.string.callRing, Toast.LENGTH_LONG).show();
					break;
				}
			}
		}, PhoneStateListener.LISTEN_CALL_STATE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
    protected void onDestroy()
    {
	    super.onDestroy();
    }

	private void startDial(final EditText dialNum, final Intent dial)
	{
		dial.putExtra(Intent.EXTRA_PHONE_NUMBER, dialNum.getText().toString());
		startActivityForResult(dial, RESULT_OK);
	}
}
